import React, { useLayoutEffect, useMemo, useRef } from "react";
import { ResourceList } from "../../Component/ResourceList";
import { FormattedMessage, useIntl } from "react-intl";
import { LibAppView } from "@/lib";
import { Button, App, Tag } from "antd";
import { PlusOutlined } from "@ant-design/icons";
import { publishOwnerList } from "./StaticData";
import { useDelete } from "../../hooks/useDelete";
import lodash from "lodash";
import { AdminRight } from "@/pages/Admin/data/AdminPageAuth";
import ResourceStatus from "../../Component/ResourceStatus";
import { useLawdocReview } from "../../Layout/Review/useLawdocReview";


const LawDocList = LibAppView.withService({
    reqEndPoints: [{
        endPoint: "setting.configList",
        name: "category",
        uri: { config: "lawDocType" }
    }]
})((props) => {
    const microApp = LibAppView.useMicroApp();
    const reviewLawdoc = useLawdocReview();
    const listRef = useRef<any>();
    const categoryList = useMemo(() => {
        const specialTypeData =  lodash.get(props.responseData, "category.data", []);
        return specialTypeData.map((item: any) => ({
            label: item.dataValue,
            value: item.name
        }));
    }, [props.responseData]);
    const deleteAction = useDelete({
        endPoint: "resource.lawDocSubmit",
        cancelEndPoint: "resource.lawDocSubmit",
        data: {},
        listRef
    });
    const columns = useMemo(() => ([{
        title: "发布主体",
        dataIndex: "publishOwnerName",
        width: 80
    }, {
        title: "分类",
        dataIndex: "categoryTitle",
        width: 100
    },
    {
        title: "标题",
        dataIndex: "title",
        render: (title: string, data: any) => {
            return (<Button type="link" style={{whiteSpace: "normal",textAlign: "left"}} onClick={() => reviewLawdoc.show(data.id)}>{title}</Button>);
        }
    },{
        title: "审核拒绝原因",
        dataIndex: "approveMsg",
        width: 200,
    },{
        title: <FormattedMessage id="admin.pages.menus.edit.status"/>,
        dataIndex: "status",
        width: 100,
        render: (status: number) => {
            return (<ResourceStatus status={status} />);
        }
    },{
        title: "创建用户",
        dataIndex: "createUserName",
        width: 120
    },{
        title: <FormattedMessage id="admin.pages.operate.edit" />,
        dataIndex: "name",
        width: 140,
        render: (name: string, data: any) => {
            const nextProps = data.status != 0 ? { disabled: true } : {};
            const cancelProps = data.status == 0 ? { disabled: true } : {};
            return (<>
                <AdminRight authRight="editable" mode="disabled" nextProps={nextProps}>
                    <Button size="small" onClick={()=>{
                        microApp.startJourney("editDetails", {
                            state: data
                        });
                    }}>
                        <FormattedMessage id="admin.pages.operate.edit"/>
                    </Button>
                </AdminRight>
                <AdminRight authRight="editable" mode="disabled" nextProps={cancelProps}>
                    <Button size="small" onClick={()=>{
                        deleteAction.cancel({
                            ...data,
                            approveMsg: "",
                            status: 0
                        })
                    }}>
                        <FormattedMessage id="admin.pages.operate.cancel"/>
                    </Button>
                </AdminRight>
                <AdminRight authRight="deleteable" mode="disabled" nextProps={nextProps}>
                    <Button size="small" onClick={() => {
                        deleteAction.delete({
                            ...data,
                            status: -1
                        });
                    }}><FormattedMessage id="admin.pages.operate.delete"/></Button>
                </AdminRight>
            </>);
        }
    }]), []);
    const searchFields = useMemo<Admin.IAdvancedField[]>(() => ([{
        label: "标题",
        name: "title",
        type: "Input"
    }, {
        label: "类别",
        name: "category",
        type: "Select",
        data: categoryList || []
    }, {
        label: "发布主体",
        name: "publishOwner",
        type: "Select",
        data: publishOwnerList
    }]), []);
    useLayoutEffect(() => {
        microApp.registeJourney({
            name: "editDetails",
            title: "Edit category",
            stepJourney: true,
            isTempJourney: true,
            steps: [{
                isMemoryPage: true,
                page: "admin.lawDocEdit"
            }]
        });
    },[]);
    return (<>
        <ResourceList
            refApi={listRef}
            tableColumns={columns}
            searchFields={searchFields}
            tableWidth={800}
            listEndPoint={{
                endPoint: "resource.lawDocList",
                formatResponse: (resp: any) => {
                    const listData: any[] = resp.data || [];
                    return listData.map((item: any) => {
                        const publishOwner = publishOwnerList.find((item2: any) => item2.value === item.publishOwner);
                        return {
                            ...item,
                            publishOwnerName: publishOwner?.label || "未知",
                        };
                    });
                }
            }}
            Operators={() => {
                return (<AdminRight authRight="editable" mode="disabled">
                    <Button type="primary" onClick={() => {
                        microApp.startJourney("editDetails");
                    }}>
                        <PlusOutlined />
                        <span><FormattedMessage id="admin.pages.operate.add" /></span>
                    </Button>
                </AdminRight>);
            }}
        />
        {reviewLawdoc.Provider}
        </>
    );
});

export default LawDocList;
